package com.restaurantonlineorder.seller.controller;

import com.restaurantonlineorder.response.BaseResult;
import com.restaurantonlineorder.seller.domain.Goods;
import com.restaurantonlineorder.seller.service.IGoodsService;
import com.restaurantonlineorder.seller.vo.req.GoodsReqVo;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @author 熊宇
 * @date 2022/7/22 23:27
 * @description 商品信息相关 控制器
 */
@Validated
@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Resource
    private IGoodsService iGoodsService;

    /**
     * 通过商家Id查询店中所有商品 若传如商品ID 则返回商品详情
     *
     * @param storeId 商店ID
     * @param goodsId 商品ID
     * @return
     */
    @GetMapping("/info")
    public BaseResult<List<Goods>> queryInfo(@NotNull(message = "商店ID不能为空") Long storeId, @RequestParam(required = false) Long goodsId) {
        List<Goods> goodsList = iGoodsService.getInfo(storeId, goodsId);
        return BaseResult.success(goodsList);
    }

    /**
     * 商家上架商品
     *
     * @param goodsReqVo
     * @return
     */
    @PostMapping("/info")
    public BaseResult putOnSaleGoodsInfo(@Valid @RequestBody GoodsReqVo goodsReqVo, HttpServletRequest request) {
        Goods goods = this.iGoodsService.save(goodsReqVo, request);
        return BaseResult.success(goods);
    }

    /**
     * 添加商品数量
     */
    @PutMapping("/quantity")
    public BaseResult addGoodsQuantity(@Valid @NotNull(message = "商品ID不能为空") Long goodsId, @Valid @NotNull(message = "商品数量不能为空") @Min(value = 1, message = "添加数量必须大于1") Integer quantity) {
        boolean isSuccess = this.iGoodsService.addGoodsQuantity(goodsId, quantity);
        return BaseResult.success(isSuccess);

    }

}
